﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using Org.BouncyCastle.Crypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class RecipeTypeService : IRecipeTypeService
    {
        private readonly IRecipeTypeOperate _recipeTypeOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public RecipeTypeService(IUnitOfWork unitOfWork, IMapper mapper , IRecipeTypeOperate recipeTypeOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _recipeTypeOperate = recipeTypeOperate;

        }


        public List<RecipeTypeModel> MendingRange(List<RecipeType> entities)
        {
            List<RecipeTypeModel> models = new List<RecipeTypeModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }

        public RecipeTypeModel Mending(RecipeType entity)
        {
            RecipeTypeModel model = _mapper.Map<RecipeTypeModel>(entity);
            return model;
        }
        public List<RecipeType> GetAllRecipeTypes()
        {
            try
            {
                return _recipeTypeOperate.GetAllRecipeTypeList();
            }
            catch (Exception ex)
            {
                return new List<RecipeType>();
            }
        }

        public RecipeType GetRecipeTypeById(string id)
        {
            try
            {
                return _recipeTypeOperate.GetRecipeTypeById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addRecipeType(RecipeType entity)
        {
            try
            {
                _recipeTypeOperate.AddRecipeType(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool updateRecipeType(RecipeType model)
        {
            try
            {
                RecipeType entity = _recipeTypeOperate.GetRecipeTypeById(model.RecipeTypeId);
                _mapper.Map(model, entity);
                _recipeTypeOperate.UpdateRecipeType(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteRecipeType(string id)
        {
            try
            {
                RecipeType entity = _recipeTypeOperate.GetRecipeTypeById(id);
                if (entity != null)
                {
                    _recipeTypeOperate.DeleteRecipeType(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelRecipeType(List<string> ids)
        {
            try
            {
                List<RecipeType> entities = _recipeTypeOperate.GetRecipeTypeByIdList(ids);
                if (entities.Count > 0)
                {
                    _recipeTypeOperate.BatchDelRecipeType(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            } 
        }
    }
}
